<?php
/**
 * SettingsController class.
 * SettingsController is for settings.
 * 
 * LBB Solutions Ltd. 版权所有 @ 2012.
 */
class SettingsController extends ApiController
{
	/**
	 * Get city list
	 */
	public function actionCity()
	{
		$lang = BUtils::T('LNG');
		if ($lang == 'fi_fi') {
			$cities = array(
				array('key'=>'capital_cities', 'value'=>'Pääkaupunkiseutu'),
			);
		} else {
			$cities = array(
				array('key'=>'capital_cities', 'value'=>'Capital Area'),
			);
		}
		
		//fetch cities form merchant accounts
		$sql_cities = 'SELECT DISTINCT city FROM tbl_merchant_account WHERE status=1 AND (city IS NOT NULL) AND country_code=:country_code ORDER BY city ';
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_cities);
		$rows = $command->queryAll(true, array(
			':country_code' => 'FI'
		));
		$connection->active = false;
		foreach ($rows as $r) {
			if (empty($r['city'])) continue;
			$cities[] = array(
				'key' => strtolower($r['city']),
				'value' => $r['city'],
			);
		}
		$this->_sendJSON(200, $cities);
	}
	
	/**
	 * Get business area list by city
	 */
	public function actionBizArea()
	{
		$city_key = Yii::app()->request->getParam('city_key');
		if (empty($city_key)) {
			$this->_sendError('The parameter "city_key" is missing!');
		}
		
		if ($city_key == 'capital_cities') {
			$sql_areas = 'SELECT biz_area AS value FROM tbl_merchant_area_city WHERE city_key IN (:city1, :city2, :city3)
				AND country_code=:country_code ORDER BY biz_area ';
			$params = array(':city1' => 'helsinki', ':city2' => 'espoo', ':city3' => 'vantaa', ':country_code' => 'FI');
		} else {
			$sql_areas = 'SELECT biz_area AS value FROM tbl_merchant_area_city WHERE city_key=:city_key AND country_code=:country_code ORDER BY biz_area ';
			$params = array(':city_key' => $city_key, ':country_code' => 'FI');
		}
		//fetch cities form merchant accounts
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_areas);
		$rows = $command->queryAll(true, $params);
		$connection->active = false;
		
		$lists = array(
			array('key'=>'all', 'value'=>BUtils::T('all_lbl')),
		);
		foreach ($rows as $r) {
			$value = trim($r['value']);
			$lists[] = array(
				'key' => strtolower($value),
				'value' => $value,
			);
		}
		$this->_sendJSON(200, $lists);
	}
	
	/**
	 * Save settings
	 */
	public function actionSave()
	{
		//get device, token
		$device = Yii::app()->request->getParam('device', null);
		$token = Yii::app()->request->getParam('token', null);
		if (empty($device) or empty($token)) {
			$this->_sendError('device or token cannot be empty!');
		}
		
		$params = array();
		//get city
		$city = Yii::app()->request->getParam('city');
		if (!empty($city)) {
			//$params[':city'] = $city;
		}
		//get discount
		$discount = Yii::app()->request->getParam('discount');
		if (!empty($discount)) {
			$params[':discount'] = abs($discount);
		}
		//get distance
		$distance = Yii::app()->request->getParam('distance');
		if (!empty($discount)) {
			$params[':distance'] = abs($distance);
		}
		
		if (count($params) > 0) {
			$connection = Yii::app()->db;
			//UPDATE device
			$params[':device_token'] = $token;
			$sql_update = 'UPDATE tbl_user_device_settings SET min_discount=:discount, max_distance=:distance, update_time=NOW()
				WHERE device_token=:device_token ';
			$command = $connection->createCommand($sql_update);
			$command->execute($params);
			$connection->active = false;
		}
		
		$this->_sendJSON(200, 'Saved!');
	}
	
	/**
	 * User send feedback to yamii.fi@gmail.com
	 */
	public function actionFeedback()
	{
		$email = Yii::app()->request->getParam('email');
		if (empty($email)) {
			$this->_sendError(BUtils::T('email_enter_empty_error_msg'));
		}
		if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
			$this->_sendJSON(500, BUtils::T('email_address_error_msg'));
		}
		
		$text = Yii::app()->request->getParam('text');
		if (empty($text)) {
			$this->_sendError(BUtils::T('feedback_eg'));
		}
		
		$sent = @BUtils::sendMaiToAdmin('Feedback from App - ' . date('Y-m-d'), strip_tags($text), $email);
		if ($sent === false) {
			$this->_sendJSON(500, BUtils::T('email_address_error_msg'));
		} else {
			$this->_sendJSON(200, BUtils::T('feedback_done_lbl'));
		}
	}
	
	/**
	 * Check the latest verison
	 */
	public function actionVersion()
	{
		$os = Yii::app()->request->getParam('device');
		if (empty($os)) {
			$this->_sendError('The parameter device is missing!');
		}
		
		$version = Yii::app()->request->getParam('version');
		if (empty($version)) {
			$this->_sendError('The parameter version is missing!');
		}
		
		$app = array(
			'version' => '1.0.0.0',
			'text' => 'Your app is the latest version.',
			'url' => 'http://yamii.fi/app',
		);
		$this->_sendJSON(200, $app);
	}
}